package deposit.main;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


public class ListDeposits {

    private List<Deposit> deposits = new ArrayList<Deposit>();

    public ListDeposits() {
	super();
    }

    public double getPrincipal() {
	double sum = 0.0;
	for (Deposit d : deposits) {
	    sum += d.getSum();
	}
	return sum;
    }

    public void init() {

	deposits.add(new SimpleDeposit("PRIVAT", "1", new Date(2012, 8, 8), 61,
		2500, 18));
	deposits.add(new MonthCapitalizeDeposit("PRIVAT", "2", new Date(2012,
		1, 1), 181, 10000, 21));
	deposits.add(new SimpleDeposit("PRIVAT", "3", new Date(2012, 10, 12),
		30, 5500, 15.3));
	deposits.add(new SimpleDeposit("PRIVAT", "4", new Date(2011, 11, 18),
		370, 43000, 19.56));
	deposits.add(new MonthCapitalizeDeposit("PRIVAT", "5", new Date(2012,
		6, 12), 91, 12000, 16)); // 73000
    }

    public void remove() {
	for (Iterator<Deposit> iter = deposits.iterator(); iter.hasNext();) {
	    if (iter.next().getSum() < 10000)
		iter.remove();
	}
    }

    public void printSortedSumAndInterest(List<Deposit> deposites) {
	Collections.sort(deposites, new ListDeposits.InterestValueSorter());
	for (Deposit d : deposites) {
	    System.out.printf("\nSum: %.2f , InterestValue: %.2f", d.getSum(),
		    d.getInterest());
	}
    }

    static class InterestValueSorter implements Comparator<Deposit> {

	@Override
	public int compare(Deposit d1, Deposit d2) {

	    if (d1.getInterest() < d2.getInterest()) {
		return 1;
	    }
	    if (d1.getInterest() > d2.getInterest()) {
		return -1;
	    }
	    return 0;
	}
    }

    public void writeSortedSumAndInterest(List<Deposit> deposites, String path) {
	PrintWriter outputStream = null;
	Collections.sort(deposites, new ListDeposits.InterestValueSorter());
	try {
	    outputStream = new PrintWriter(new FileWriter(path));
	    for (Deposit d : deposites) {
		outputStream.write(String.format(
			"Sum: %1$9.2f  InterestValue: %1$6.2f\r\n", d.getSum(),
			d.getInterest()));

	    }
	} catch (IOException ex) {
	    ex.printStackTrace();
	}

	finally {
	    if (outputStream != null) {
		outputStream.close();
	    }
	}
    }

    public void saveDeposits(List<Deposit> deposits, String path) {
	ObjectOutputStream out;
	try {
	    out = new ObjectOutputStream(new FileOutputStream(path));

	    for (Deposit d : deposits) {

		out.writeObject(d);

	    }

	} catch (FileNotFoundException e1) {
	    // TODO Auto-generated catch block
	    e1.printStackTrace();
	} catch (IOException e1) {
	    // TODO Auto-generated catch block
	    e1.printStackTrace();
	} finally {
	}
    }

    public List<Deposit> readObject(String path) {
	List<Deposit> list = new ArrayList<>();

	try {
	    ObjectInputStream in = new ObjectInputStream(new FileInputStream(
		    path));

	    while (true) {
		Deposit d = (Deposit) in.readObject();
		list.add(d);
	    }

	} catch (EOFException ex) {
	    return list;
	} catch (ClassCastException ex) {
	    ex.printStackTrace();
	} catch (ClassNotFoundException ex) {
	    ex.printStackTrace();
	} catch (IOException ex) {
	    ex.printStackTrace();
	}

	return null;
    }

    void add100(int index) {
	try {
	    
	    double sum = deposits.get(index).getSum(); //read
	    Thread.sleep(1000);
	    deposits.get(index).setSum(sum + 100); //set
	    
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
    
    public double getSum(int index){
	Deposit depo = deposits.get(index);
	return depo.getSum();
}
}
